package com.teacold.customdemo.chapter_thirteen;

import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Build;
import android.provider.Settings;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.teacold.customdemo.R;

import androidx.appcompat.app.AppCompatActivity;

public class GestureActivity extends AppCompatActivity implements View.OnTouchListener, View.OnClickListener {
    private TextView gesture_text;
    private GestureDetector mGestureDetector;
    private Button gesture_add, gesture_remove;
    private WindowManager mWindowManager;
    private ImageView mImageView;
    private WindowManager.LayoutParams mLyoutParams;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gesture);
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
            Intent myIntent=new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
            startActivity(myIntent);
            initView();
        }else {
            initView();
        }

    }

    private void initView() {
       mWindowManager= (WindowManager) getSystemService(Context.WINDOW_SERVICE);

//        mGestureDetector=new GestureDetector(new MyGestureListener());
        mGestureDetector = new GestureDetector(new MySimpleGestureListener());
        gesture_text = findViewById(R.id.gesture_text);
        gesture_text.setOnTouchListener(this);
        gesture_text.setFocusable(true);
        gesture_text.setClickable(true);
        gesture_text.setLongClickable(true);
        gesture_add = findViewById(R.id.gesture_add);
        gesture_remove = findViewById(R.id.gesture_remove);
        gesture_add.setOnClickListener(this);
        gesture_remove.setOnClickListener(this);


    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return mGestureDetector.onTouchEvent(event);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.gesture_add:
                addWindow();
                break;
            case R.id.gesture_remove:
                removeWindow();
                break;
        }
    }

    private void addWindow(){
        mImageView=new ImageView(this);
        mImageView.setImageResource(R.mipmap.ic_launcher_round);
        mLyoutParams=new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,2099,
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED,
                PixelFormat.TRANSPARENT);
        mLyoutParams.type=WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
        mLyoutParams.gravity=Gravity.TOP|Gravity.LEFT;
        mLyoutParams.x=0;
        mLyoutParams.y=300;
        mImageView.setOnTouchListener(this);
        mWindowManager.addView(mImageView,mLyoutParams);
    }
    private void removeWindow(){
        mWindowManager.removeViewImmediate(mImageView);

    }

}
